package com.resou.reader.mine.paycenter;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.resou.reader.R;
import com.resou.reader.api.entry.PayConfigBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RechargePriceAdapter extends RecyclerView.Adapter<RechargePriceAdapter.PriceHolder> {
    private List<PayConfigBean> configs;

    public void setConfigs(List<PayConfigBean> configs) {
        this.configs = configs;
        notifyDataSetChanged();
    }

    private OnPriceAdapterItemClickListener mListener;

    public void setListener(OnPriceAdapterItemClickListener listener) {
        mListener = listener;
    }

    @NonNull
    @Override
    public PriceHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_pay_config, viewGroup, false);
        return new PriceHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PriceHolder holder, int position) {
        PayConfigBean bean = configs.get(position);
        holder.tvMoney.setText(String.valueOf((int) bean.getCnyPrice() + holder.itemView.getResources().getString(R.string.unit_yuan)));
        //热币
        holder.tvCurrency.setText(String.valueOf((int) bean.getBookBean() + holder.itemView.getResources().getString(R.string.currency)));
        //热券
        int giftBean = (int) bean.getGiftBean();
        if (giftBean > 0) {
            String voucherStr = String.valueOf("送 " + giftBean + holder.itemView.getResources().getString(R.string.voucher));
            holder.tvVoucher.setText(voucherStr);
        } else {
            holder.tvVoucher.setText("");
        }
        if (bean.getSuperscript() == 1 && position == 0) {//首充角标
//            holder.icCornerMark.setVisibility(View.VISIBLE);
            holder.icCornerMark.setVisibility(View.VISIBLE);
            holder.icCornerMark.setImageResource(R.mipmap.ic_first_recharge);
        } else if (position == getItemCount() - 1) {
            //会员功能暂未上线，暂时隐藏送会员图标
//            holder.icCornerMark.setVisibility(View.VISIBLE);
//            holder.icCornerMark.setImageResource(R.drawable.ic_handsel_vip);
        }

        holder.itemView.setOnClickListener(v -> {
            mListener.onItemClicked(bean);
        });
    }

    @Override
    public int getItemCount() {
        return configs == null ? 0 : configs.size();
    }

    class PriceHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_money)
        TextView tvMoney;
        @BindView(R.id.tv_currency)
        TextView tvCurrency;
        @BindView(R.id.tv_voucher)
        TextView tvVoucher;
        @BindView(R.id.ic_corner_mark)
        ImageView icCornerMark;
        @BindView(R.id.pay_config_container)
        ConstraintLayout payConfigContainer;

        public PriceHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    interface OnPriceAdapterItemClickListener {
        void onItemClicked(PayConfigBean bean);
    }
}
